package com.hxtec.polaris.controller;

import com.hxtec.polaris.commons.api.vo.Result;
import com.hxtec.polaris.commons.contants.CommonContants;
import com.hxtec.polaris.entity.ShopCartItem;
import com.hxtec.polaris.service.CartItemService;
import com.hxtec.polaris.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author Caps
 * @date 2019/11/25
 */
@RestController
@RequestMapping("cart")
@CrossOrigin
@Api(value = "/cart", tags = "购物车")
public class CartController {

    @Autowired
    private CartItemService cartService;

    @ApiOperation(value = "根据sku获取商品详添加购物车")
    @PostMapping("addToCart/{sku}/{quantity}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sku", value = "商品的sku", required = true, dataType = "String"),
            @ApiImplicitParam(name = "quantity", value = "商品的sku", required = true, dataType = "String"),
    })
    public Result<?> addToCart(@PathVariable int sku,@PathVariable int quantity){
        Map<String, Object> map = cartService.addTocart(sku, quantity);
        return Result.ok(map);
    }


    @ApiOperation(value = "获取购物车列表")
    @GetMapping("cartList")
    public Result<?> cartList(){
        List<ShopCartItem> cartItemList= cartService.getCartList();
        return Result.ok(cartItemList);
    }

    @ApiOperation(value = "获取购物车选择的购物数据")
    @GetMapping("getCartCheckedGoods")
    public Result<?> getCartCheckedGoods(String cartIdData){
        List<ShopCartItem> cartCheckedGoods = cartService.getCartCheckedGoods(cartIdData);
        return Result.ok(cartCheckedGoods);
    }

    @ApiOperation(value = "删除购物车")
    @DeleteMapping("deleteCart")
    public Result<?> deleteCart(String cartIdData){
        Map<String, Object> map = cartService.deleteCart(cartIdData);
        return Result.ok(map);
    }
}

